#!bin/usr/R
# Thomas J. Leeper
# Aarhus University
# Created 2012-02-26
# Updated 2013-07-26

##########################################################################
### "The Informational Basis for Mass Polarization" Analysis (Study 1) ###
##########################################################################

library(car)
library(coin)
library(xtable)
data <- read.csv("study1-data-final-2012-06-08.csv")

source("coefpaste.r")
source("expResults.r")
.groupNames <- c("Search Pro","Search Con","Captive Pro","Captive Con","Control")

### Create Outcome Measures
#define binary prior opinion measures
data$hc1.bin <- recode(data$hc1,"lo:.49999999=-1;.5=0;.500000001:1=1;else=NA")
data$libya1.bin <- recode(data$libya1,"lo:.49999999=-1;.5=0;.500000001:1=1;else=NA")

#define prior importance measures
data$hc1imp.bin <- recode(data$hc1.imp,"lo:.49999999=-1;.5=0;.500000001:1=1;else=NA")
data$libya1imp.bin <- recode(data$libya1.imp,"lo:.49999999=-1;.5=0;.500000001:1=1;else=NA")

#Change from t1 to t2
data$chg.hc21 <- data$hc2-data$hc1
data$chg.libya21 <- data$libya2-data$libya1

###Create SEARCH VARIABLES
#note: these variables only apply to search conditions

#search total clicks
data$search.hcpro <- with(data,X11+X12+X13+X14)	#hc-pro (11-14)
data$search.hccon <- with(data,X19+X20+X21+X22)	#hc-con (19-22)
data$search.libyapro <- with(data,X15+X16+X17+X18)	#libya-pro (15-18)
data$search.libyacon <- with(data,X23+X24+X25+X26)	#libya-con (23-26)
data$search.filler <- with(data,X27+X28+X29+X30+X31+X32+X33+X34)	#fluff (27-34)
data$search.total <- with(data,search.hcpro+search.hccon+search.libyapro+search.libyacon+search.filler)

#proportions
data$sprop.hcpro <- data$search.hcpro/data$search.total
data$sprop.hccon <- data$search.hccon/data$search.total
data$sprop.libyapro <- data$search.libyapro/data$search.total
data$sprop.libyacon <- data$search.libyacon/data$search.total
data$sprop.filler <- data$search.filler/data$search.total

################
### ANALYSIS ###
################

##Search data
#hc
sprohc1con <- with(subset(data,hc1.bin==-1),expResults(sprop.hcpro,conditions))
sprohc1pro <- with(subset(data,hc1.bin==1),expResults(sprop.hcpro,conditions))
sconhc1con <- with(subset(data,hc1.bin==-1),expResults(sprop.hccon,conditions))
sconhc1pro <- with(subset(data,hc1.bin==1),expResults(sprop.hccon,conditions))

searchtab1 <- data.frame(rbind(
		#pro hits among those with Con and Pro t1 opinions
		cbind(mapply(coefpaste,sprohc1con[,1],sprohc1con[,4]),mapply(coefpaste,sprohc1pro[,1],sprohc1pro[,4]))[1,],
		#con hits among those with Con and Pro t1 opinions
		cbind(mapply(coefpaste,sconhc1con[,1],sconhc1con[,4]),mapply(coefpaste,sconhc1pro[,1],sconhc1pro[,4]))[2,]))
names(searchtab1) <- c("t1 con","t1 pro")
rownames(searchtab1) <- c("Search Pro","Search Con")

#libya
sprolibya1con <- with(subset(data,libya1.bin==-1),expResults(sprop.libyapro,conditions))
sprolibya1pro <- with(subset(data,libya1.bin==1),expResults(sprop.libyapro,conditions))
sconlibya1con <- with(subset(data,libya1.bin==-1),expResults(sprop.libyacon,conditions))
sconlibya1pro <- with(subset(data,libya1.bin==1),expResults(sprop.libyacon,conditions))

searchtab2 <- data.frame(rbind(
		#pro hits among those with Con and Pro t1 opinions
		cbind(mapply(coefpaste,sprolibya1con[,1],sprolibya1con[,4]),mapply(coefpaste,sprolibya1pro[,1],sprolibya1pro[,4]))[1,],
		#con hits among those with Con and Pro t1 opinions
		cbind(mapply(coefpaste,sconlibya1con[,1],sconlibya1con[,4]),mapply(coefpaste,sconlibya1pro[,1],sconlibya1pro[,4]))[2,]))
names(searchtab2) <- c("t1 con","t1 pro")
rownames(searchtab2) <- c("Search Pro","Search Con")

# combined latex output for healthcare and libya search
xtable(cbind(searchtab1,searchtab2),label="tab:search1",caption="Issue-Relevant Articles as Proportion of Total Articles Read, by Search Condition and Prior Opinion (Study 1)",align=c("l","r","r","r","r"))


## Opinion
hc1.results <- with(data,expResults(hc1,conditions))
hc2.results <- with(data,expResults(hc2,conditions))
hc21chg.results <- with(data,expResults(chg.hc21,conditions))

#latex results for HC (Conds 1-4,7)
hcresults <- data.frame(cbind(
				mapply(coefpaste,hc1.results[,1],hc1.results[,4]),
				mapply(coefpaste,hc2.results[,1],hc2.results[,4]),
				mapply(coefpaste,hc21chg.results[,1],hc21chg.results[,4])))
names(hcresults) <- c("t1","t2","t2-t1")
#xtable(hcresults)

libya1.results <- with(data,expResults(libya1,conditions))
libya2.results <- with(data,expResults(libya2,conditions))
libya21chg.results <- with(data,expResults(chg.libya21,conditions))

#latex results for Libya (Conds 1-4,7)
libyaresults <- data.frame(cbind(
				mapply(coefpaste,libya1.results[,1],libya1.results[,4]),
				mapply(coefpaste,libya2.results[,1],libya2.results[,4]),
				mapply(coefpaste,libya21chg.results[,1],libya21chg.results[,4])))
names(libyaresults) <- c("t1","t2","t2-t1")
#xtable(libyaresults)

#latex change results for HC and Libya
chgs <- data.frame(cbind(as.character(hcresults[,3]),as.character(libyaresults[,3])))
names(chgs) <- c("Healthcare","Libya")
rownames(chgs) <- rownames(hcresults)
xtable(chgs,label="tab:chg1",align=c("l","r","r"),caption="Healthcare and Libya Opinion Changes by Treatment Condition (Study 1)")


## significance tests
with(data,kruskal.test(hc1~conditions))
with(data,kruskal.test(hc2~conditions))
with(data,kruskal.test(chg.hc21~conditions))

with(data,kruskal.test(libya1~conditions))
with(data,kruskal.test(libya2~conditions))
with(data,kruskal.test(chg.libya21~conditions))


# plot of change over time for ONLINE APPENDIX
ltime <- cbind(
	with(data,expResults(libya1,conditions))[,1],
	with(data,expResults(libya2,conditions))[,1],
	with(data,expResults(libya3,conditions))[,1]
)

htime <- cbind(
	with(data,expResults(hc1,conditions))[,1],
	with(data,expResults(hc2,conditions))[,1],
	with(data,expResults(hc3,conditions))[,1]
)

pdf("EffectDuration.pdf",width=7,height=4)
layout(matrix(1:2,1,2,byrow=TRUE))
par(lwd=2)
plot(ltime[1,],col="blue",lty=2,type="l",ylim=c(0.3,0.9),xaxt="n",yaxt="n",bty="l",xlab="Time",ylab="Mean Opinion",main="Libya",mar=c(5,4,2,0))
axis(1,1:3,c("Pre-test","Post-test","Follow-up"))
axis(2,seq(0,1,by=.1),seq(0,1,by=.1),las=1)
	lines(ltime[5,],col="gray")
	lines(ltime[4,],col="red")
	lines(ltime[3,],col="blue")
	lines(ltime[2,],col="red",lty=2)
plot(htime[1,],col="blue",lty=2,type="l",ylim=c(0.3,0.9),xaxt="n",yaxt="n",bty="l",xlab="Time",ylab="",main="Healthcare",mar=c(5,1,2,2))
axis(1,1:3,c("Pre-test","Post-test","Follow-up"))
axis(2,seq(0,1,by=.1),seq(0,1,by=.1),las=1)
	lines(htime[5,],col="gray")
	lines(htime[4,],col="red")
	lines(htime[3,],col="blue")
	lines(htime[2,],col="red",lty=2)
dev.off()
